package com.liangfeng.ysdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.liangfeng.ysdemo.EzvizApplication;
import com.liangfeng.ysdemo.R;
import com.liangfeng.ysdemo.util.LogUtils;
import com.liangfeng.ysdemo.util.ToastUtils;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.realplay.RealPlayStatus;
import com.videogo.util.ConnectionDetector;
import com.videogo.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by mzf on 2017/4/18.
 */

public class PreviewActivity extends AppCompatActivity {

    private EZOpenSDK openSDK;
    private ArrayList<EZDeviceInfo> deviceList;
    private SurfaceView sv_preview;
    private String TAG = "PreviewActivity";
    private List<EZCameraInfo> cameraInfoList;
    private EZPlayer mEZPlayer;
    private EZDeviceInfo mDeviceInfo;

    /**
     * 标识是否正在播放
     */
    private int mStatus = RealPlayStatus.STATUS_INIT;
    private boolean mIsOnStop = false;
    private Handler mHandler;
    private String pwd;
    private ProgressBar pb_loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);

        Button btn_finish = (Button) findViewById(R.id.btn_finish);
        pb_loading = (ProgressBar) findViewById(R.id.pb_loading);
        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(PreviewActivity.this);
        final AlertDialog alertDialog = builder.create();
        //创建后立即显示
        alertDialog.show();
        //设置软键盘可见
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //设置对话框的布局
        View view = LayoutInflater.from(PreviewActivity.this).inflate(R.layout.dialog_account, null);
        alertDialog.setContentView(view);
        //设置对话框不可被取消
        alertDialog.setCancelable(false);

        final EditText et_pwd = (EditText) view.findViewById(R.id.et_input_dialog);
        Button btn_confirm_dialog = (Button) view.findViewById(R.id.btn_confirm_dialog);
        Button btn_cancel_dialog = (Button) view.findViewById(R.id.btn_cancel_dialog);
        //确定按钮的点击事件
        btn_confirm_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                //显示加载中
                pb_loading.setVisibility(View.VISIBLE);
                pwd = et_pwd.getText().toString().trim();
                Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {


                    @Override
                    public void call(Subscriber<? super String> subscriber) {

                        //获取EZOpenSDK的实例
                        openSDK = EzvizApplication.getOpenSDK();
                        //
                        cameraInfoList = mDeviceInfo.getCameraInfoList();
                        //预览前必须设置密码
                        if (!TextUtils.isEmpty(pwd)) {
                            startRealPlay(pwd);
                        }

                    }
                });

                Subscriber<String> subscriber = new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {

                    }
                };

                //订阅
                observable.subscribeOn(Schedulers.io())
                        .observeOn(Schedulers.io())
                        .subscribe(subscriber);

            }
        });
        //取消按钮的点击事件
        btn_cancel_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Intent intent = getIntent();
        mDeviceInfo = (EZDeviceInfo) intent.getParcelableExtra("deviceInfo");

        sv_preview = (SurfaceView) findViewById(R.id.sv_preview);
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS:
                        LogUtils.e("播放成功");
                        pb_loading.setVisibility(View.GONE);
                        break;
                    case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL:
                        LogUtils.e("播放失败:" + msg + "<<<<<<<<<<");
                        pb_loading.setVisibility(View.GONE);
                        ToastUtils.showLong(PreviewActivity.this,"播放失败请检查网络");
                        break;
                }
            }
        };



    }

    /**
     * 开始播放
     *
     * @see
     * @since V2.0
     */
    private void startRealPlay(String pwd) {
        // 增加手机客户端操作信息记录
        LogUtil.debugLog(TAG, "startRealPlay");

        if (mStatus == RealPlayStatus.STATUS_START || mStatus == RealPlayStatus.STATUS_PLAY) {
            return;
        }

        // 检查网络是否可用
        if (!ConnectionDetector.isNetworkAvailable(this)) {
            // 提示没有连接网络
            ToastUtils.showLong(this, "请检查网络");
            return;
        }

        EZCameraInfo mCameraInfo = cameraInfoList.get(0);
        if (mCameraInfo != null) {
            if (mEZPlayer == null) {
                mEZPlayer = EzvizApplication.getOpenSDK().createPlayer(mCameraInfo.getDeviceSerial(), mCameraInfo.getCameraNo());
            }

            if (mEZPlayer == null)
                return;
            if (mDeviceInfo == null) {
                return;
            }
            if (mDeviceInfo.getIsEncrypt() == 1) {
                mEZPlayer.setPlayVerifyCode(pwd);
            }
            mEZPlayer.setHandler(mHandler);
            mEZPlayer.setSurfaceHold(sv_preview.getHolder());
            mEZPlayer.startRealPlay();
        }
    }


}
